<template>
  <div
    class="login"
    :style="{ height: maxHeight + 'px' }"
    :class="[setTheme == 1 ? 'blue' : 'red']"
  >
    <div class="login-form">
      <i class="el-icon-close close" @click="handleClose"></i>
      <h3 class="title">{{ $t("langName.langName1") }}</h3>
      <el-form
        :model="userForm"
        :rules="loginRules"
        ref="ruleForm"
        :label-width="languageType == 1 ? '170px' : '140px'"
      >
        <!-- 账号 -->
        <el-form-item :label="$t('langName.langName11')" prop="nickname">
          <el-input
            v-model="userForm.nickname"
            :placeholder="$t('langName.langName16')"
          >
            <i slot="prefix" class="el-icon-s-custom img-size"></i
          ></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item :label="$t('langName.langName12')" prop="password">
          <el-input
            v-model="userForm.password"
            type="password"
            :placeholder="$t('langName.langName17')"
            :show-password="true"
          >
            <i slot="prefix" class="el-icon-unlock img-size"></i>
          </el-input>
        </el-form-item>
        <!-- 确认密码 -->
        <el-form-item :label="$t('langName.langName13')" prop="nextPassword">
          <el-input
            v-model="userForm.nextPassword"
            type="password"
            :placeholder="$t('langName.langName18')"
            :show-password="true"
          >
            <i slot="prefix" class="el-icon-unlock img-size"></i>
          </el-input>
        </el-form-item>
        <!-- 头像 -->
        <el-form-item :label="$t('langName.langName36')" style="width: 350px">
          <div v-if="isDefaultAvatar">
            <img
              src="@/assets/images/morentouxiang.png"
              alt=""
              width="60"
              height="60"
            />
          </div>
          <el-upload
            class="upload-demo"
            action="#"
            :with-credentials="true"
            :on-change="getFile"
            list-type="picture"
            :limit="1"
            ref="onload"
            :http-request="beforeAvatarUpload"
          >
            <el-button
              size="small"
              :type="setTheme == 1 ? 'primary' : 'danger'"
              style="margin-right: 10px; font-size: 16px"
              @click="handleUploadAvator"
              >{{ $t("langName.langName14") }}</el-button
            >
          </el-upload>
        </el-form-item>
        <!-- 注册按钮 -->
        <el-button
          :type="setTheme == 1 ? 'primary' : 'danger'"
          class="submitBtn"
          @click="handleRegister('ruleForm')"
          >{{ $t("langName.langName37") }}</el-button
        >
      </el-form>
    </div>
  </div>
</template>

<script>
import { postRegister } from "@/api/collect";
import { getBase64 } from "@/utils/location";
import Cookies from "js-cookie";
export default {
  data() {
    const validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error(this.$t("langName.langName16")));
      } else {
        if (this.userForm.nextPassword !== "") {
          this.$refs.ruleForm.validateField("nextPassword");
        }
        callback();
      }
    };

    const validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error(this.$t("langName.langName18")));
      } else if (value != this.userForm.password) {
        callback(new Error(this.$t("langName.langName34")));
      } else {
        callback();
      }
    };
    return {
      maxHeight: "",
      userForm: {
        //表单数据
        nickname: "",
        password: "",
        nextPassword: "",
        avatar: "",
        avatarname: "",
        language: "",
      },
      loginRules: {
        //验证规则
        nickname: [
          {
            required: true,
            message: this.$t("langName.langName16"),
            trigger: "blur",
          },
        ],
        password: [
          {
            required: true,
            message: this.$t("langName.langName17"),
            trigger: "blur",
          },
          {
            min: 6,
            max: 15,
            message: "长度在 6 到 15 个字符",
            trigger: "blur",
          },
          { validator: validatePass, trigger: "blur" },
        ],
        nextPassword: [
          {
            required: true,
            message: this.$t("langName.langName18"),
            trigger: "blur",
          },
          {
            min: 6,
            max: 15,
            message: "长度在 6 到 15 个字符",
            trigger: "blur",
          },
          { validator: validatePass2, trigger: "blur", required: true },
        ],
      },
      isDefaultAvatar: true, //是否显示默认头像
      languageType: Cookies.get("lang"), //语言类型
    };
  },
  created() {
    this.setTheme = localStorage.getItem("setTheme")
      ? localStorage.getItem("setTheme")
      : "1";
  },
  mounted() {
    this.maxHeight =
      window.innerHeight ||
      document.documentElement.clientHeight ||
      document.body.clientHeight;
  },

  methods: {
    // 关闭注册页
    handleClose() {
      this.$router.go(-1);
    },

    // 重新上传头像
    handleUploadAvator() {
      this.$refs.onload.clearFiles();
    },

    // 上传头像获取头像地址
    getFile(file, fileList) {
      this.userForm.avatarname = file.name;
      this.isDefaultAvatar = false;
      getBase64(file.raw).then((res) => {
        this.userForm.avatar = res;
      });
    },
    beforeAvatarUpload(file) {
      this.avatarNewname = file.file.name;
    },

    // 注册
    async handleRegister(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          this.userForm.language = Cookies.get("lang");
          this.userForm.avatar = this.userForm.avatar
            ? this.userForm.avatar
            : "";
          this.userForm.avatarname = this.userForm.avatarname
            ? this.userForm.avatarname
            : "morentouxiang.png";
          let { status, message } = await postRegister(this.userForm);
          if (status == 200) {
            this.$message({
              message: this.$t("langName.langName38"),
              type: "success",
            });
            this.$router.push({
              path: "/login",
            });
          } else {
            this.$message({
              message: message,
              type: "error",
            });
          }
        } else {
        }
      });
    },
  },
};
</script>
<style lang="scss" scoped>
@import "../assets/css/login.css";

::v-deep .el-upload {
  position: absolute;
  left: 100px;
  top: 10px;
}

::v-deep .el-upload-list {
  width: 60px;
  height: 60px;
  border-radius: 50%;
}
::v-deep .el-upload-list__item:first-child {
  margin-top: 0;
}
::v-deep .el-form-item__content {
  display: flex;
}

::v-deep .el-form-item {
  margin: 15px;
}
.el-upload-list--picture .el-upload-list__item-thumbnail {
  width: 60px;
  height: 60px;
}
</style>
